//
// Created by DMC on 2024/5/11.
//

// 工厂模式
#include <iostream>

// 水果类
class Fruit {
public:
    virtual ~Fruit() = default;
    virtual void show() = 0;
};

class Apple : public Fruit {
public:
    void show() override {
        std::cout << "Apple" << std::endl;
    }
};

class Banana : public Fruit {
public:
    void show() override {
        std::cout << "Banana" << std::endl;
    }
};

class FruitFactory {
public:
    virtual ~FruitFactory() = default;
    static Fruit *createFruit(const std::string &type) {
        if (type == "apple") {
            return new Apple();
        } else if (type == "banana") {
            return new Banana();
        } else {
            return nullptr;
        }
    }
};